import java.awt.Rectangle;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JPasswordField;
import javax.swing.JScrollPane;
import javax.swing.JScrollBar;
import javax.swing.JComboBox;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.MouseEvent;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;

/**
 * 
 * this class is statistic data
 *
 */
public class SData extends JPanel {
	
	private JComboBox ComboPer = null;
	private JComboBox mngBox = null;
	private JComboBox CmbChoose = null;
	private JLabel mngOptLabel = null;
	/**
	 * title for the current window  
	 */
	private JLabel lblTitle = null;
	/**
	 * select button
	 */
	private JButton btnSelect = null;
	/**
	 * back button
	 */
	public JButton btnBack = null;
	/**
	 * help button
	 */
	public JButton btnHelp = null;
	private String TypeFlag = "Teacher";
	private String PerFlag = "Class";
	/**
	 *  checks if a supported query has been asked, true for good, false for unsupported one
	 */
	private boolean flag = false;
	/**
	 *  AdminUI gui
	 */
	private AdminUI admUI = null;
	/**
	 *  ManUI gui
	 */
	private ManUI manUI = null;
	/**
	 *  SecUI gui
	 */
	private SecUI secUI = null;
	/**
	 *  ManagerUI gui
	 */
	private ManagerUI ManagerUI = null;
	/**
	 * save the current panel for the "back" function 
	 */
	private JPanel curPanel;

	public SData() {
		super();
		initialize();
	}


	private void initialize() {
		curPanel = this;

		
		mngOptLabel = new JLabel();
		mngOptLabel.setBounds(new Rectangle(46, 71, 80, 19));
		mngOptLabel.setText("Grades of:");
	
		btnSelect = new JButton();
		btnSelect.setBounds(new Rectangle(191, 216, 95, 25));
		btnSelect.setText("Select");

		
		lblTitle = new JLabel("Statistical Data");
		lblTitle.setBounds(154, 29, 146, 14);

		
		this.setSize(400, 321);
		this.setLayout(null);
		
		this.add(mngOptLabel);
		this.add(lblTitle);
		this.add(btnSelect(),null);
		this.add(getMngBox(), null);
		this.add(getComboPer(), null);
		this.add(getCmbChoose(), null);
		
		btnBack = new JButton("Back");
		btnHelp = new JButton("Help");
		btnHelp.setBounds(390, 11, 80, 19);
		btnHelp.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				  HelpWin help = new HelpWin ("Choose the type of Statistical data you want to see","and click on 'Select' button ");
				  System.out.println("user press Help button");
			}
		});
		this.add(btnHelp,null);
		this.setVisible(true);
		
		btnBack.setBounds(191, 296, 86, 25);
		this.add(btnBack,null);
		
				
		JLabel LablePer = new JLabel();
		LablePer.setText("Per:");
		LablePer.setBounds(new Rectangle(46, 71, 48, 19));
		LablePer.setBounds(223, 72, 48, 19);
		add(LablePer);
		
		JLabel lblChoose = new JLabel("Choose which one:");
		lblChoose.setBounds(42, 143, 146, 16);
		add(lblChoose);
		this.setVisible(true);
		
	}
	
	private JComboBox getMngBox() {
		if (mngBox == null) {
			mngBox = new JComboBox();
			mngBox.setBounds(new Rectangle(46, 102, 95, 17));
			mngBox.addItem("Teachers");
			mngBox.addItem("Class");
			mngBox.addItemListener(new java.awt.event.ItemListener() {
				public void itemStateChanged(java.awt.event.ItemEvent e) {
					System.out.println("itemStateChanged()"); 
					TypeFlag = mngBox.getSelectedItem().toString();
					System.out.println("this is : " + TypeFlag); 
					SQL s = new SQL(mngBox.getSelectedItem().toString(),36);
					s.accept();
					Object msg = s.getMsg();
					LinkedList<String> list = (LinkedList<String>)msg;
					String str = list.pollFirst();
					CmbChoose.removeAllItems();
					while (str != null)
					{
						CmbChoose.addItem(str);
						str = list.pollFirst();
					}
				}
			});
		}
		return mngBox;
	}
	
	private JComboBox getComboPer() {
		if (ComboPer == null) {
			ComboPer = new JComboBox();
			ComboPer.setBounds(new Rectangle(223, 102, 135, 17));
			ComboPer.addItem("Class");
			ComboPer.addItem("Teacher");
			ComboPer.addItem("Course");
			
			ComboPer.addItemListener(new java.awt.event.ItemListener() {
				public void itemStateChanged(java.awt.event.ItemEvent e) {
					System.out.println("itemStateChanged()"); 
					PerFlag = ComboPer.getSelectedItem().toString();
					System.out.println("this is : " + PerFlag); 
				}
			});
		}
		return ComboPer;
	}
	
	private JComboBox getCmbChoose() {
		if (CmbChoose == null) {
			CmbChoose = new JComboBox();
			CmbChoose.setBounds(46, 172, 131, 22);
			TypeFlag = mngBox.getSelectedItem().toString();
			SQL s = new SQL(mngBox.getSelectedItem().toString(),36);
			s.accept();
			Object msg = s.getMsg();
			LinkedList<String> list = (LinkedList<String>)msg;
			String str = list.pollFirst();
			while (str != null)
			{
				CmbChoose.addItem(str);
				str = list.pollFirst();
			}
			}
			//CmbChoose.addItem("Teachers");
		
			CmbChoose.addItemListener(new java.awt.event.ItemListener() {
				public void itemStateChanged(java.awt.event.ItemEvent e) {
					System.out.println("itemStateChanged()"); 
					TypeFlag = mngBox.getSelectedItem().toString();
					System.out.println("this is : " + TypeFlag); 
				}
			});
		
		return CmbChoose;
	}
	
	

	
	private JButton btnSelect() {
		//CmbChoose.getSelectedItem()
		this.btnSelect.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				flag = false;
				String selection = new String();
				selection = TypeFlag+" "+PerFlag;
				System.out.println("this is the selected: " + selection);
				if (selection.equals("Teachers Class"))
				{
					flag = true;
					System.out.println("Now Teacher per Classes data should show");
					Object temp[]= {"teacherid","classid",CmbChoose.getSelectedItem()};
					String output = Global.SetOutput(temp);
					Global.StatShow(output, 30, 50, "Grades of teacher with id: "+CmbChoose.getSelectedItem()+" per classes");
				}
				
				if (selection.equals("Class Teacher"))
				{
					flag = true;
					System.out.println("Now Class per Teachers data should show");
					Object temp[]= {"classid","teacherid",CmbChoose.getSelectedItem()};
					String output = Global.SetOutput(temp);
					Global.StatShow(output, 30, 50, "Grades of class with id: "+CmbChoose.getSelectedItem()+" per teacheres");				}
				
				if (selection.equals("Class Course"))
				{
					flag = true;
					System.out.println("Now Class per Courses data should show");
					Object temp[]= {"classid","courseid",CmbChoose.getSelectedItem()};
					String output = Global.SetOutput(temp);
					Global.StatShow(output, 30, 50, "Grades of class with id: "+CmbChoose.getSelectedItem()+" per teacher");			
				}
				if (!flag) Global.StatShow("This query is not supported in this version", 20, 30, " Sorry ");
			}
		});
		return btnSelect;
	}
} 


